home *** CD-ROM | disk | FTP | other *** search
/ Cream of the Crop 22 / Cream of the Crop 22.iso / program / ctlib100.zip / INSTALL.LZH / STCKQUE.TXT < prev    next >
Text File  |  1996-10-12  |  4KB  |  60 lines

  1.                        CHAPTER 9
  2.                    Stacks and Queues
  3.  
  4.  
  5. This chapter discusses in detail the stack and queue objects included in the Containers Library.  It describes how to use these objects and the different types of stacks and queues available.
  6.  
  7. In this chapter you will learn:
  8.  
  9.   - What are stacks?
  10.   - The general characteristics of stacks
  11.   - What are queues?
  12.   - The general characteristics of queues
  13.  
  14.  
  15. What are stacks?
  16.  
  17. A stack is formally defined as a list in which all insertions and deletions are made at one end of the list, usually referred to as the top of the list.  When an element is inserted in a stack, it is said it was pushed into the list.  When an element is deleted from a stack, it is said it was popped from the list.
  18.  
  19. Stacks are known as LIFO or last-in-first-out lists, which means that the first item added to the list will always be the last one to be removed.  This is useful for such things as eliminating recursive procedures in tree structures or writing compilers, for example.
  20.  
  21.  
  22. Using the stacks
  23.  
  24. All stacks inherit their functionality from other more complex containers like the arrays, the collections and the linked lists.  How you use a stack depend on the container on which it is based.  For example, the TArrayStack uses a resizable dynamic array to store items in the list, while TLinkedStack uses a TList container to build a linked list of items.  All stacks implement the following additional methods used to access the data in the stack:
  25.  
  26.   - Push    : inserts an item at the top of the stack
  27.   - Pop     : removes an item from the top of the stack
  28.   - Top     : returns the item at the top of the stack
  29.   - Bottom  : returns the item at the bottom of the stack
  30.  
  31. Since data items are stored in memory at all times, the use of DoneItem is not required.  However, if you intend to write data-structure independent applications, you must still always use it after retrieving an item from the container.
  32.  
  33.  
  34. What are queues?
  35.  
  36. Queues are lists in which all insertions to the list are made at one end of the list, and deletions are made at the other end.  The end at which insertions are made is usually referred to as the front and the end at which deletions are made is usually referred to as the rear.
  37.  
  38. Queues are also known as FIFO or first-in-first-out lists, which means that the first item added to the list will always be the first to be removed.  These structures are useful for such things as background printer jobs.
  39.  
  40. Only two queues are included in the Containers Library, both of which are linked lists. The double-ended queue lets you access items from either end of the list.
  41.  
  42.  
  43. Using the queues
  44.  
  45. Queues are TList descendants that work in the same way as linked list containers.  However, additional methods are used to provide the functionality of the queue:
  46.  
  47.   - EnQueue : inserts an item at the rear (end) of the queue.
  48.   - Remove  : removes an item from the front of the queue.
  49.   - Front   : returns the first item in the queue (the first
  50.               item that will be removed).
  51.   - Rear    : returns the last item in the queue (the last
  52.               item to that will be removed).
  53.  
  54. The double ended queue also provides the following methods:
  55.  
  56.   - RemoveFirst : removes the first item in the queue
  57.   - RemoveLast  : removes the last item in the queue
  58.  
  59. Since data items are stored in memory at all times, the use of DoneItem is not required.  However, if you intend to write data-structure independent applications, you must still always use it after retrieving an item from the container.
  60.